﻿using Catastrophe.Common.FileM;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace Catastrophe.Log.UIMode
{
    /// <summary>
    /// 
    /// </summary>
    public partial class SystemExceptionFrmMode:ObservableObject
    {
        /// <summary>
        /// 是否显示
        /// </summary>
        [ObservableProperty]
        private bool isAllowSetNeverShow;

        /// <summary>
        /// 提示信息
        /// </summary>
        [ObservableProperty]
        private string infoStr;
        /// <summary>
        /// 异常缓存
        /// </summary>
        public  ExceptionCache _exceptionCache;
        public ICommand Refresh { get; set; }
        public ICommand Clear { get; set; }
        public ICommand Save { get; set; }

        public SystemExceptionFrmMode()
        {
            Refresh=new RelayCommand(RefreshMethod);
            Clear = new RelayCommand(ClearMethod);
            Save = new RelayCommand(SaveMethod);
        }

        private void SaveMethod()
        {
            try
            {
                DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(6, 1);
                defaultInterpolatedStringHandler.AppendLiteral("Error_");
                defaultInterpolatedStringHandler.AppendFormatted(DateTime.Now, "yyyyMMddHHmmssfff");
                string fileName = defaultInterpolatedStringHandler.ToStringAndClear();
                SaveFileDialog saveFileDialog = new SaveFileDialog
                {
                    DefaultExt = ".txt",
                    FileName = fileName
                };
                var dialogResult = saveFileDialog.ShowDialog();
                if (dialogResult == true)
                {
                    FileUtil.SaveFileSteam(infoStr, saveFileDialog.FileName);
                    MessageBox.Show("保存成功!");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void ClearMethod()
        {
            this._exceptionCache.RemoveAll();
        }

        private void RefreshMethod()
        {
            infoStr= _exceptionCache.ToString();
        }
    }
}
